package com.yanqu.road.dao.impl.activity.toyhouse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.toyhouse.ToyHouseOrder;
import com.yanqu.road.entity.activity.toyhouse.ToyHouseUserData;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class ToyHouseUserDataDaoImpl extends TempDataObjectDao<ToyHouseUserData> {

    public static String getToyHouseUserDataSql = "select * from `t_u_activity_toy_house_user` where `activityId`=? and `userId`=?";

    public ToyHouseUserData getToyHouseUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getToyHouseUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateToyHouseUserDataSql = "update `t_u_activity_toy_house_user` set score=?,candyCount=?,energy=?,recoveryTime=?,guideIndex=?,blockDataMap=?,orderMap=?,toyProp=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(ToyHouseUserData toyHouseUserData) {
        toyHouseUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, toyHouseUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, toyHouseUserData.getCandyCount()));
        para.put(i++, new DbParameter(Types.INTEGER, toyHouseUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, toyHouseUserData.getRecoveryTime()));
        para.put(i++, new DbParameter(Types.INTEGER, toyHouseUserData.getGuideIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(toyHouseUserData.getBlockDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(toyHouseUserData.getOrderMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(toyHouseUserData.getToyProp())));
        para.put(i++, new DbParameter(Types.INTEGER, toyHouseUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, toyHouseUserData.getUserId()));
        if (getDb().executeNoneQuery(updateToyHouseUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertToyHouseUserDataSql = "insert into `t_u_activity_toy_house_user`(activityId,userId,score,candyCount,energy,recoveryTime,guideIndex,blockDataMap,orderMap,toyProp) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(ToyHouseUserData toyHouseUserData) {
        toyHouseUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, toyHouseUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, toyHouseUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, toyHouseUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, toyHouseUserData.getCandyCount()));
        para.put(i++, new DbParameter(Types.INTEGER, toyHouseUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, toyHouseUserData.getRecoveryTime()));
        para.put(i++, new DbParameter(Types.INTEGER, toyHouseUserData.getGuideIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(toyHouseUserData.getBlockDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(toyHouseUserData.getOrderMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(toyHouseUserData.getToyProp())));
        if (getDb().executeNoneQuery(insertToyHouseUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ToyHouseUserData parseData(ResultSet rs) throws SQLException {
        ToyHouseUserData toyHouseUserData = new ToyHouseUserData();
        toyHouseUserData.setActivityId(rs.getInt("activityId"));
        toyHouseUserData.setUserId(rs.getLong("userId"));
        toyHouseUserData.setScore(rs.getLong("score"));
        toyHouseUserData.setCandyCount(rs.getLong("candyCount"));
        toyHouseUserData.setEnergy(rs.getInt("energy"));
        toyHouseUserData.setRecoveryTime(rs.getLong("recoveryTime"));
        toyHouseUserData.setGuideIndex(rs.getInt("guideIndex"));
        toyHouseUserData.setBlockDataMap(JSON.parseObject(rs.getString("blockDataMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.toyhouse.ToyHouseBlockData>>() {
        }));
        toyHouseUserData.setOrderMap(JSON.parseObject(rs.getString("orderMap"), new TypeReference<Map<Integer, ToyHouseOrder>>() {
        }));
        toyHouseUserData.setToyProp(PropertyHelper.parseStringToProperty(rs.getString("toyProp")));
        return toyHouseUserData;
    }

}